package com.sy.cloud.wechat.service;

import com.sy.cloud.wechat.ConstantsPlaceholder;
import com.sy.cloud.wechat.WeChatConfig;
import com.sy.cloud.wechat.WeChatException;
import com.sy.cloud.wechat.entity.WeChatEntity;
import com.sy.cloud.wechat.entity.message.NewsMessage;
import com.sy.cloud.wechat.entity.message.TemplateMessage;
import com.sy.cloud.wechat.entity.message.TextMessage;
import com.sy.cloud.wechat.utils.HttpUtils;
import com.sy.common.service.ITestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by zhuwenquan on 2017/9/9.
 */
@Service
@Slf4j
public class MessageService {

    @Resource
    ITestService testService;

    public boolean sendTextMessage(final String weixinId, final String openId, final String content) {
        log.info( "send text1 " + content  );
        TextMessage textMessage = new TextMessage();
        textMessage.setOpenId(openId);
        textMessage.setContent(content);
        String requestJson = textMessage.toReturnJSON();



        String apiUrl = WeChatConfig.getUrl("send.message").replace(ConstantsPlaceholder.ACCESS_TOKEN, testService.getAccesstoken(weixinId) );
        WeChatEntity entity = HttpUtils.post(requestJson, apiUrl, WeChatEntity.class);

        log.info( "send text3 " + requestJson  );
        log.info( "apiUrl" + apiUrl );

        if (entity.isSuccess()) {
            return Boolean.TRUE;
        } else {
            log.info( "send text error " + entity.toErrorString()  );
            throw new WeChatException("send text error " + entity.toErrorString());
        }
    }

    public boolean sendNewsMessage(final String accessToken,
                                   final String openId,
                                   final String title,
                                   final String description,
                                   final String url,
                                   final String picurl) {
        NewsMessage newsMessage = new NewsMessage();
        newsMessage.setOpenId(openId);
        NewsMessage.News news = new NewsMessage.News();
        news.addArticle(new NewsMessage.News.Article(title, description, url, picurl));
        newsMessage.setNews(news);
        String requestJson = newsMessage.toReturnJSON();

        String apiUrl = WeChatConfig.getUrl("send.message").replace(ConstantsPlaceholder.ACCESS_TOKEN, accessToken);

        WeChatEntity entity = HttpUtils.post(requestJson, apiUrl, WeChatEntity.class);

        if (entity.isSuccess()) {
            return Boolean.TRUE;
        } else {
            throw new WeChatException("send news error " + entity.toErrorString());
        }
    }
    public Boolean sendTemplateMsg(final String accessToken,
                                   final String openId, String templateId, String url, Map<String, String> params, String titleColor, String valueColor) {
        String apiUrl = WeChatConfig.getUrl("send.template.message").replace(ConstantsPlaceholder.ACCESS_TOKEN, accessToken);

        TemplateMessage message = new TemplateMessage();
        message.setTouser(openId);
        message.setTemplate_id(templateId);
        message.setTopcolor(titleColor);
        message.setUrl(url);
        Iterator response = params.entrySet().iterator();

        while (response.hasNext()) {
            Map.Entry entry = (Map.Entry) response.next();
            message.addKeyValue((String) entry.getKey(), (String) entry.getValue(), valueColor);
        }
        String requestJson = message.toJSON();//.toReturnJSON();
        WeChatEntity entity = HttpUtils.post(requestJson, apiUrl, WeChatEntity.class);

        if (entity.isSuccess()) {
            return Boolean.TRUE;
        } else {
            throw new WeChatException("send news error " + entity.toErrorString());
        }
    }

}
